package com.integrationmessagers.telegram.services;

import com.integrationmessagers.telegram.config.properties.TelegramProperty;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.telegram.telegrambots.api.methods.send.SendMessage;
import org.telegram.telegrambots.api.objects.Update;
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
import org.telegram.telegrambots.exceptions.TelegramApiException;

@Component
@RequiredArgsConstructor
public class AnalyzerLongPollingBot extends TelegramLongPollingBot {

    private final TelegramProperty telegramProperty;

    @Override
    public void onUpdateReceived(Update update) {

        if (update.hasMessage() && update.getMessage().hasText()) {
            String firstName = update.getMessage().getChat().getFirstName();
            String chatId = update.getMessage().getChatId().toString();
            String message = "Привет, зая, цом, цом, цом"; // + firstName;

            sendMessage(chatId, message);
        }

    }

    private synchronized void sendMessage(String chatId, String text) {
        SendMessage message = new SendMessage();

        message.enableMarkdown(true);
        message.setChatId(chatId);
        message.setText(text);

        try {
            execute(message);
        } catch (TelegramApiException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public String getBotUsername() {
        return telegramProperty.getVkAnalyzerBotName();
    }

    @Override
    public String getBotToken() {
        return telegramProperty.getVkAnalyzerBotToken();
    }
}
